package org.xhnews.bo;

import org.xhnews.constant.Constant;
import org.xhnews.download.DownloadManager;
import org.xhnews.preferences.Preferences;
import org.xhnews.utils.DeviceUtil;
import org.xhnews.utils.StringUtil;

public class AuthenticationBo {
	/*
	 * 登陆服务器
	 */
	public static boolean loginServer() {
		String url = Preferences.server_address+Constant.URL_LOGIN + "IMEI=" + DeviceUtil.getPhoneIMEI();
		String responseStr = StringUtil.is2String(DownloadManager.send(url));
		if (responseStr == null) {
			return false;
		} else if (responseStr.indexOf("OK") != -1) {
			// 登录成功
			return true;
		}
		return false;
	}
    /*
     * 新用户注册
     */
	public static int regiser(String sn) {
		String url = Preferences.server_address+Constant.URL_REGISTER + "IMEI="
				+ DeviceUtil.getPhoneIMEI() + "&MNUM=" + sn;
		String res = StringUtil.is2String(DownloadManager.send(url));
		if (res == null) {
			// 无法访问服务器
			return Constant.RES_CONNECTION_FAILED;
		} else if (res.indexOf("OK") != -1) {
			Preferences.registered = true;
			Preferences.registration_sn = sn;
			Preferences.save();
			return Constant.RES_REGISTRATION_SUCC;
			// 注册成功
		} else if (res.indexOf("ER") != -1) {
			String[] subRes = StringUtil.split(res, ':');// ER:此手机未注册:1
			int errCode = Integer.parseInt(subRes[1]);
			switch (errCode) {
			case 1:// ER:此手机未授权:1
				return Constant.RES_UNAUTHORIZED;
			case 2:// ER:此手机被禁用:2
				// 提示用户该手机的服务被禁用
				return Constant.RES_DISABLED;
			case 3:// ER:服务已过期:3
				// 提示用户该手机的服务已过期
				return Constant.RES_OVERDUE;
			case 4:// ER:4:此手机号码已与其他手机绑定
				// 提示用户此手机号码已与其他手机绑定
				return Constant.RES_REGISTER_BY_OTHER_DEVICE;
			default:
				return Constant.RES_UNKNOWN;
			}
		}
		return Constant.RES_UNKNOWN;
	}
}
